package mode;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;


import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;



public class GamePanel extends JPanel {
    int bg_y=1;
    BufferedImage bg;
    int bgtp=1;
    int score;//分数
    Heroji hero = new Heroji();
    //Diji dj = new Diji();
    //敌机
    List<Ep> eps = new ArrayList<Ep>();
    //子弹
    List<Fire> fs=new ArrayList<Fire>();
    //游戏开关
    boolean gameover;
    int index=0;
    public void action(){
        new Thread(){
            public void run(){
                while(true){
                    if(!gameover){
                        index++;
                        if(index%15==0){
                            EpEnter();
                        }
                        //EpEnter();
                        EpMove();
                        //发射子弹
                        shoot();
                        //子弹移动
                        fireMove();
                        //子弹命中
                        shootEp();
                        //敌机撞机
                        hit();
                    }
                    try {
                        Thread.sleep(15);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    repaint();
                }
            }


        }.start();
    }
    protected void hit() {
        for(int i=0;i<eps.size();i++){
            Ep e=eps.get(i);
            if(e.hitBy(hero)){
                eps.remove(e);
                hero.hp--;
                if(hero.hp==0){
                    gameover=true;
                }
            }
        }

    }

    protected void shootEp() {
        for(int i=0;i<fs.size();i++){
            Fire f=fs.get(i);
            band(f);
        }

    }
    ;
    private void band(Fire f) {
        for(int i=0;i<eps.size();i++){
            Ep e=eps.get(i);
            if(e.shootBy(f)){
                if (e.type!=15){
                    e.hp--;
                    if(e.hp<0){
                        if (e.type==14&&hero.hp<5){
                            hero.hp++;
                        }
                        if (e.type==13&&hero.hp<5){
                            hero.hp++;
                        }
                        eps.remove(e);
                        score+=17-e.sp;
                        if (score%50==0&&score!=0){
                            bgtp++;

                        }

                    }

                }fs.remove(f);

            }
        }

    }
    protected void fireMove() {
        for(int i=0;i<fs.size();i++){
            Fire f = fs.get(i);
            f.move();
        }


    }
    int findex=0;
    protected void shoot() {
        findex++;
        if(findex>=5){
            if(score>=0&&score<100||score>=400){
                Fire fire3 =new Fire(hero.x+33,hero.y-20,0);
                fs.add(fire3);
            }if(score>=100&&score<400){
                Fire fire7 =new Fire(hero.x+3,hero.y-20,0);
                fs.add(fire7);

                Fire fire6 =new Fire(hero.x+63,hero.y-20,0);
                fs.add(fire6);
            }if(score>=400){
                Fire fire2 =new Fire(hero.x+23,hero.y-10,-1);
                fs.add(fire2);

                Fire fire4 =new Fire(hero.x+53,hero.y-10,1);
                fs.add(fire4);
            }if(score>=2000){
                Fire fire1 =new Fire(hero.x+3,hero.y,-2);
                fs.add(fire1);

                Fire fire5 =new Fire(hero.x+63,hero.y,2);
                fs.add(fire5);
            }findex=0;
        }

    }
    public void EpMove() {
        for(int i=0;i<eps.size();i++){
            Ep ep = eps.get(i);
            ep.move();
        }

    }
    //int index=0;
    protected void EpEnter() {

        //index++;
        //if(index>=20){
        Ep e = new Ep();
        eps.add(e);

        //}
        //index=0;

    }
    public GamePanel(){


        MouseAdapter adapter = new MouseAdapter(){
            //重新开始游戏
            @Override
            public void mouseClicked(MouseEvent e) {
                if (gameover){
                    hero=new Heroji();
                    gameover=false;
                    score=0;
                    bgtp=1;
                    bg_y=1;
                    eps.clear();
                    fs.clear();
                    repaint();
                }
            }

            //鼠标控制
            public void mouseMoved(MouseEvent e) {
                int mx=e.getX();
                int my=e.getY();
                if(!gameover){
                    hero.moveToMouse(mx, my);
                }


                repaint();
                //try{
                //	Thread.sleep(15);
                //}catch(InterruptedException d){
                //	d.printStackTrace();
                //}
            }

        };
        addMouseListener(adapter);
        addMouseMotionListener(adapter);
    }

    @Override
    public void paint(Graphics g) {

        setBackground(Color.black);
        bg = App.getImg("/img/bg"+bgtp%5+".jpg");


        super.paint(g);


        g.drawImage(bg, 0,  bg_y, 512, 768 + bg_y,  //图片坐标
                0, 0, 512, 768, null);       //图片放在窗口的坐标

        g.drawImage(bg, 0, -768 + bg_y, 512,  bg_y, 0, 0, 512, 768, null);
        bg_y += 1;
        if (bg_y >= 768) {
            bg_y = 1;
        }
        g.drawImage(hero.img,hero.x,hero.y,hero.w,hero.h,null);


        //画敌机
        for(int i=0;i<eps.size();i++){
            Ep ep = eps.get(i);
            g.drawImage(ep.img, ep.x, ep.y, ep.w, ep.h,null);
        }
        //g.drawImage(ep.img, ep.x, ep.y, ep.w, ep.h,null);
        //画子弹
        for(int i=0;i<fs.size();i++){
            Fire fire = fs.get(i);
            g.drawImage(fire.img, fire.x, fire.y, fire.w, fire.h,null);
        }
        g.setColor(Color.white);
        g.setFont(new Font("楷体",Font.BOLD,25));
        g.drawString("分数"+score,10,30);

        //画血量
        for(int i=0;i<hero.hp;i++){
            g.drawImage(hero.img, 320+4*35-i*35, 5,30, 30, null);
        }

        if(gameover){
            g.setColor(Color.red);
            g.setFont(new Font("楷体",Font.BOLD,100));
            g.drawString("游戏结束",25,300);
            g.setColor(Color.orange);
            g.setFont(new Font("楷体",Font.BOLD,45));
            g.drawString("点击屏幕重新开始",40,350);
        }

    }
}
